//index.js
//获取应用实例
import { formatDate, isNumber } from '../../utils/util'

const app = getApp()

Page({
  data: {
    verno: app.globalData.verno,
    logined: false,
    nickName: '',
    avatarUrl: '',
    CustomBar: app.globalData.CustomBar,

    noteType: '支出',
    isExpense: true,
    expenseTypes: [],
    expenseLists: [],
    expenseType: '',
    expenseDate: '',
    expenseMoney: '',
    listType: 'expenseType',
    focusExpenseType: true,
    loading: false,

    isExpenseUpdate: false,
    expenseUpdateId: '',
    expenseUpdateType: '',
    expenseUpdateMoney: '',

    isCenter: false,
    centerList: [
      { text: '查找记录', icon: 'cuIcon-search text-orange', to: '/pages/search/index' },
      { text: '按月统计', icon: 'cuIcon-rank text-olive', to: '/pages/byMonth/index' },
      { text: '按年统计', icon: 'cuIcon-rankfill text-cyan', to: '/pages/byYear/index' }
    ],
  },

  onLoad: async function () {
    this.setData({
      expenseDate: formatDate(new Date())
    })
    this.onLogined(wx.getStorageSync('userInfo') || undefined)
  },
  onShow: function () {
  },

  onLogin(e) {
    var that = this
    if (that.data.logined) {
      return
    }
    wx.getUserProfile({
      desc: "用于小程序的用户注册",
      success: function (info) {
        app.post({
          action: 'login',
          data: info.userInfo
        }).then(res => {
          that.onLogined(res.data)
        })
      },
      fail: console.log
    })
  },
  onLogined: function (res) {
    if (res) {
      wx.setStorageSync('userInfo', res)
      this.setData({
        logined: true,
        nickName: res.nickName,
        avatarUrl: res.avatarUrl,
        expenseTypes: wx.getStorageSync('expenseTypes'),
      })
      this.getExpenseList()
    }
  },
  doNothing(e) { },
  toCenter(e) {
    this.setData({ isCenter: true })
  },
  hideCenter(e) {
    this.setData({ isCenter: false })
  },
  centerItemTap(e) {
    wx.navigateTo({
      url: `${this.data.centerList[e.currentTarget.id].to}?isExpense=${this.data.isExpense}`,
    })
  },
  exit(e) {
    wx.clearStorageSync()
    app.globalData.logined = false
    wx.redirectTo({
      url: '/pages/home/index',
    })
    this.setData({
      isCenter: false,
      logined: false,
      nickName: '',
      avatarUrl: '',
    })
  },
  noteTypeChange(e) {
    this.setData({
      noteType: e.detail.value ? '支出' : '收入',
      isExpense: e.detail.value,
      expenseType: '',
      expenseMoney: '',
      expenseTypes: [],
      expenseLists: [],
    })
    this.getExpenseList()
    this.getExpenseType()
  },
  expenseTypeFocus(e) {
    //console.log('expenseTypeFocus')
    this.setData({
      listType: 'expenseType'
    })
  },
  expenseMoneyFocus(e) {
    //console.log('expenseMoneyFocus')
    this.setData({
      listType: 'expenseList'
    })
  },
  expenseTypeChange(e) {
    //console.log(e)
    this.setData({ expenseType: e.detail.value })
  },
  expenseDateChange(e) {
    this.setData({ expenseDate: e.detail.value })
    this.getExpenseList()
  },
  expenseMoneyChange(e) {
    if (isNumber(e.detail.value)) {
      this.setData({ expenseMoney: e.detail.value })
    } else {
      this.setData({ expenseMoney: this.data.expenseMoney })
    }
  },
  expenseAdd(e) {
    if (this.data.expenseMoney.length > 0 && this.data.expenseType.length > 0) {
      this.addExpense()
    } else {
      wx.showToast({
        title: "类型和金额不能为空",
        icon: 'none',
      })
    }
  },
  expenseTypeTap(e) {
    //console.log(e)
    this.setData({
      focusExpenseType: false,
      expenseType: e.currentTarget.dataset.type
    })
  },
  expenseListTap(e) {
    this.setData({
      isExpenseUpdate: true,
      expenseUpdateId: e.currentTarget.dataset.id,
      expenseUpdateType: e.currentTarget.dataset.type,
      expenseUpdateMoney: e.currentTarget.dataset.money
    })
  },
  expenseUpdateTypeChange(e) {
    this.setData({ expenseUpdateType: e.detail.value })
  },
  expenseUpdateMoneyChange(e) {
    if (isNumber(e.detail.value)) {
      this.setData({ expenseUpdateMoney: e.detail.value })
    } else {
      this.setData({ expenseUpdateMoney: this.data.expenseUpdateType })
    }
  },
  expenseDelete(e) {
    this.expenseUpdateHide(e)
    this.deleteExpense()
  },
  expenseUpdateHide(e) {
    this.setData({ isExpenseUpdate: false })
  },
  expenseUpdateCommit(e) {
    this.expenseUpdateHide(e)
    this.updateExpense()
  },

  bindRefresh: async function () {
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true,
      refresh: true,
    })
    await this.getExpenseList()
    await this.getExpenseType()
    this.setData({
      loading: false,
      refresh: false
    })
  },
  getExpenseList: async function (e) {
    app.post({
      action: 'getExpenseList',
      data: {
        isExpense: this.data.isExpense,
        startDt: this.data.expenseDate,
        endDt: this.data.expenseDate,
      }
    }).then(res => {
      res.data.forEach(item => {
        item.money = item.money.toFixed(2)
      })
      this.setData({
        expenseLists: res.data || []
      })
    })
  },
  getExpenseType: async function (e) {
    app.post({
      action: 'getExpenseType',
      data: { isExpense: this.data.isExpense }
    }).then(res => {
      this.setData({
        expenseTypes: res.data || []
      })
      wx.setStorageSync('expenseTypes', res.data)
    })
  },
  addExpense: async function (e) {
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true,
    })
    app.post({
      action: 'addExpense',
      data: {
        isExpense: this.data.isExpense,
        type: this.data.expenseType,
        date: this.data.expenseDate,
        money: this.data.expenseMoney
      }
    }).then(res => {
      wx.showToast({
        title: res.message,
        icon: 'none',
      })
      this.setData({
        loading: false,
        focusExpenseType: true,
        expenseType: '',
        expenseMoney: ''
      })
      this.getExpenseList()
    }).catch(res => {
      wx.showToast({
        title: res.message,
        icon: 'none',
      })
      this.setData({
        loading: false
      })
    })
  },
  updateExpense: async function (e) {
    app.post({
      action: 'updateExpense',
      data: {
        isExpense: this.data.isExpense,
        _id: this.data.expenseUpdateId,
        type: this.data.expenseUpdateType,
        money: this.data.expenseUpdateMoney
      }
    }).then(res => {
      wx.showToast({
        title: res.message,
        icon: 'none',
      })
      this.getExpenseList()
    })
  },
  deleteExpense: async function (e) {
    app.post({
      action: 'deleteExpense',
      data: {
        isExpense: this.data.isExpense,
        _id: this.data.expenseUpdateId
      }
    }).then(res => {
      wx.showToast({
        title: res.message,
        icon: 'none',
      })
      this.getExpenseList()
    })
  }
})
